package Controller.UserController;

import Model.User;
import Service.UserService;
import Service.UserServiceImpl;
import Utils.ServiceFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author zyp
 * date 2022-03-08
 */
@WebServlet("/register.do")
public class RegisterServlet extends HttpServlet {
    UserService userService = ServiceFactory.getUserService();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");

        String username = req.getParameter("register_username");
        String password = req.getParameter("register_password");

        User user = new User();
        user.setUsername(username);
        user.setPassword(password);

        if (userService.register(user)) {
            resp.getWriter().println("<script>alert('恭喜您注册成功');window.location.href='index.html' </script>");
        } else {
            resp.getWriter().println("<script>alert('注册失败，请重新尝试注册');window.location.href='index.html' </script>");
        }
    }
}
